package com.css.fxfzfxqh.modules.ysx.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.css.fxfzfxqh.base.dict.service.DictService;
import com.css.fxfzfxqh.base.response.RestResponse;

import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.util.YSXEntity;
import com.css.fxfzfxqh.modules.util.repository.YSXRepository;
import com.css.fxfzfxqh.modules.ysx.entity.FxqhYSX;
import com.css.fxfzfxqh.modules.ysx.param.YSXParam;
import com.css.fxfzfxqh.modules.ysx.service.YSXService;
import com.css.fxfzfxqh.util.CreateGson;
import com.css.fxfzfxqh.util.UUIDGenerator;
import com.css.fxfzfxqh.zcpt.sys.entity.SUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/08/22
 * @Description: ysx
 */
@Service
@Slf4j
public class YSXServiceImpl implements YSXService {

    @Resource
    private YSXRepository ysxRepository;
    @Resource
    DictService dictService;

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, YSXParam param) {
        //结构类型转换
        if (null!=param.getStructureType()){
            //城镇钢混结构房屋格网
            if (param.getStructureType().equals("czghjggw")){
                if (null!=param.getNumberOfFloors()){
                    //1-8层
                    if (Integer.parseInt(param.getNumberOfFloors())>=1&Integer.parseInt(param.getNumberOfFloors())<8){
                        param.setStructureType("钢筋混凝土结构-框架结构");
                    }
                    //8-18层
                    if (Integer.parseInt(param.getNumberOfFloors())>=8&Integer.parseInt(param.getNumberOfFloors())<18){
                        param.setStructureType("钢筋混凝土结构-框架-剪力墙结构");
                    }
                    //>18层
                    if (Integer.parseInt(param.getNumberOfFloors())>=18){
                        param.setStructureType("钢筋混凝土结构-剪力墙结构");
                    }
                }
            }
            //城镇木结构房屋格网  农村木竹结构房屋格网
            if (param.getStructureType().equals("czmjggw")|param.getStructureType().equals("ncmzjggw")){
                param.setStructureType("木结构");
            }
            //城镇其他结构房屋格网  农村混杂结构房屋格网  农村其他结构房屋格网
            if (param.getStructureType().equals("czqtjglxgw")|param.getStructureType().equals("nchzjggw")|param.getStructureType().equals("ncqtjglxgw")){
                param.setStructureType("其他");
            }
            //农村钢混结构房屋格网
            if (param.getStructureType().equals("ncghjggw")){
                param.setStructureType("钢筋混凝土结构-框架结构");
            }
            //城镇砌体结构房屋格网  农村底部框架结构房屋格网  农村砌体结构房屋格网
            if (param.getStructureType().equals("czqtjggw")|param.getStructureType().equals("ncdbkjkzqqtjggw")|param.getStructureType().equals("ncqtjggw")){
                param.setStructureType("砌体结构-砖混结构");
            }
            //农村钢结构房屋格网 城镇钢结构房屋格网
            if (param.getStructureType().equals("ncgjggw")|param.getStructureType().equals("czgjggw")){
                param.setStructureType("钢结构");
            }
            //农村土木石木结构房屋格网
            if (param.getStructureType().equals("nctmsmjggw")){
                param.setStructureType("土木结构");
            }
        }
        //写入设防烈度
        if (null!=param.getFortify()) {
            String valueByDictCodeAndKey = dictService.getValueByDictCodeAndKey("sfqk", param.getFortify());
            param.setFortify(valueByDictCodeAndKey);
        }
        return ysxRepository.getPage(curPage,pageSize,param);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse uploadYsxInformation(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<List<Object>> read = reader.read(2, reader.getRowCount());
            for (List<Object> objects : read) {
                YSXEntity ysx = new YSXEntity();
                FxqhYSX fxqhYSX = new FxqhYSX();
                ysx.setId(UUIDGenerator.getUUID());
                ysx.setProvince_id(objects.get(0).toString());
                ysx.setCity_id(objects.get(1).toString());
                ysx.setCounty_id(objects.get(2).toString());
                ysx.setStructure_type(objects.get(3).toString());
                ysx.setFortify(objects.get(4).toString());
                ysx.setDamage_degree(objects.get(5).toString());
                ysx.setIntensity6(Double.valueOf(objects.get(6).toString()));
                ysx.setIntensity7(Double.valueOf(objects.get(7).toString()));
                ysx.setIntensity8(Double.valueOf(objects.get(8).toString()));
                ysx.setIntensity9(Double.valueOf(objects.get(9).toString()));
                ysx.setIntensity10(Double.valueOf(objects.get(10).toString()));

                fxqhYSX.setId(UUIDGenerator.getUUID());
                fxqhYSX.setProvinceId(objects.get(0).toString());
                fxqhYSX.setCityId(objects.get(1).toString());
                fxqhYSX.setCountyId(objects.get(2).toString());
                fxqhYSX.setStructureType(objects.get(3).toString());
                fxqhYSX.setFortify(objects.get(4).toString());
                fxqhYSX.setDamageDegree(objects.get(5).toString());
                fxqhYSX.setIntensity6(Double.valueOf(objects.get(6).toString()));
                fxqhYSX.setIntensity7(Double.valueOf(objects.get(7).toString()));
                fxqhYSX.setIntensity8(Double.valueOf(objects.get(8).toString()));
                fxqhYSX.setIntensity9(Double.valueOf(objects.get(9).toString()));
                fxqhYSX.setIntensity10(Double.valueOf(objects.get(10).toString()));
                ysxRepository.saveYsx(ysx,fxqhYSX);

            }

            return RestResponse.succeed("导入成功!");
        }catch (Exception e) {
            String message = "模板数据异常,请检查!";
            log.error(message, e);
            return RestResponse.fail(message);
        }
    }
}
